package com.bbs.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bbs.entity.Message;
import com.bbs.entity.User;
import com.bbs.service.MessageService;
import com.bbs.util.Page;

public class MessageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_method");
		if("findmine".equals(method)){
			findMine(request,response);
		}
	}

	private void findMine(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User u = (User) request.getSession().getAttribute("user");
		if(u == null){
			response.sendRedirect("index.jspx?_method=login");
		} else {
			Page page = new Page();
			page.setTotleSize(new MessageService().getNoReadCountByuid(u.getId()));
			if(request.getAttribute("p") != null){
				page.setCurrentPage(Integer.parseInt(request.getParameter("p")));
			}
			List<Message> msglist = new MessageService().findNoReadByUid(u.getId(),page);
			if(msglist != null){
				new MessageService().setRead();
			} else {
				msglist = new ArrayList<Message>();
			}
			request.getSession().setAttribute("msglist", msglist);
			response.sendRedirect("index.jspx?_method=usercenter");
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
